# To change this template, choose Tools | Templates
# and open the template in the editor.

from game.ai.action import Action

class AttackAction(Action):

    def __init__(self):
        Action.__init__(self)

    def perform(self, dat, src, dst):
        
        #find the attacker by checking the occupant of the src tile
        s_tile = dat.level.tiles[src[0]][src[1]]
        atk = s_tile.occupant

        #no attacker, don't do anything
        if atk == None:
            return None

        #check the defender by finding the occupant of the dst tile
        nx = src[0] + dst[0]
        ny = src[1] + dst[1]

        d_tile = dat.level.tiles[nx][ny]
        dfd = d_tile.occupant

        #no defender, don't do anything (implement non-char attacking)
        if dfd == None:
            dat.gui.msg_mgr.add_msg("There is nothing to attack!")
            return None

        s_vis = s_tile.visible == 'full'
        d_vis = d_tile.visible == 'full'

        ar = atk.attack_rating()
        dr = dfd.reflex()

        cth = ar - dr
        if cth <= 0:
            if s_vis and d_vis:
                dat.gui.msg_mgr.add_msg("{0} miss{1} {2}!".format(
                    atk.subj_name,
                    ("es" if atk.subj_plural else ""),
                    dfd.objv_name
                ))
            elif s_vis:
                dat.gui.msg_mgr.add_msg("{0} seem{1} to be attacking something!".format(
                    atk.subj_name,
                    ("s" if atk.subj_plural else "")
                ))
            elif d_vis:
                dat.gui.msg_mgr.add_msg("{0} seem{1} to be dodging something!".format(
                    dfd.subj_name,
                    ("s" if dfd.subj_plural else "")
                ))
            dat.sched_mgr.rep_char(atk, 10)
            return None

        b_dmg = atk.base_damage()
        mit = dfd.mitigate()

        f_dmg = max(b_dmg - mit, 0)

        hp_left = dfd.damage_attr("hp", f_dmg)

        if s_vis and d_vis:
            dat.gui.msg_mgr.add_msg("{0} attack{1} {2} for {3} damage!".format(
                atk.subj_name,
                ("s" if atk.subj_plural else ""),
                dfd.objv_name,
                f_dmg)
            )
        elif s_vis:
            dat.gui.msg_mgr.add_msg("{0} seem{1} to be attacking something!".format(
                atk.subj_name,
                ("s" if atk.subj_plural else "")
            ))
        elif d_vis:
            dat.gui.msg_mgr.add_msg("{0} seem{1} to be hit by something!".format(
                dfd.subj_name,
                ("s" if dfd.subj_plural else "")
            ))

        if hp_left <= 0:
            dfd.die()
            dat.level.tiles[dfd.x][dfd.y].occupant = None
            dat.char_sprites.remove(dfd)
            if d_vis:
                dat.gui.msg_mgr.add_msg(" {0} die{1}.".format(
                    dfd.subj_name,
                    ("s" if dfd.subj_plural else "")
                ))

        dat.sched_mgr.rep_char(atk, 10)
        return None